package mortal.game.control
{
    import Framework.MQ.*;
    import Message.BroadCast.*;
    import Message.Command.*;
    import Message.Public.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MapUIController extends Controller
    {

        public function MapUIController()
        {
            
        }

        protected function onSceneUpdate(event:DataEvent) : void
        {
            cache.mapui.onSceneChangeAfter();
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).updateMap(cache.mapui.currentMapId);
            }
            
        }

        private function onMapFindPath(event:DataEvent) : void
        {
            cache.mapui.mapFindPath = event.data as Array;
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).updatePath(cache.mapui.mapFindPath);
            }
            
        }

        private function onMapUpdateRolePosition(event:DataEvent) : void
        {
            cache.mapui.currentPosition = event.data as AStarPoint;
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).updatePostion(Game.scene.rolePlayer.currentPoint);
            }
            
        }

        private function onChangePosition(event:DataEvent) : void
        {
            cache.mapui.currentPosition = event.data as AStarPoint;
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).changePosition(cache.mapui.currentPosition);
            }
            
        }

        private function onRoleMoveEnd(event:PlayerEvent) : void
        {
            cache.mapui.currentPosition = RolePlayer.instance.currentPoint;
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).onMoveEnd(cache.mapui.currentPosition);
            }
            
        }

        private function onConveyRes(event:DataEvent) : void
        {
            cache.mapui.currentPosition = RolePlayer.instance.currentPoint;
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).onConveyEnd(cache.mapui.currentPosition);
            }
            
        }

        private function onSceneChange(event:DataEvent) : void
        {
            cache.mapui.onSceneChangeBefore();
            if (_view)
            {
                (_view as IMapUIModule).sceneChangeBefore();
            }
            
        }

        private function onEntityEnter(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SEntityInfo;
            cache.mapui.entityEnter(_loc_2);
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).entityEnter(_loc_2);
            }
            
        }

        private function onEntityUpdate(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SEntityMoveInfo;
            var _loc_3:* = cache.mapui.entityUpdate(_loc_2);
            if (_loc_3 != null && _view && !_view.isHide)
            {
                (_view as IMapUIModule).entityUpdate(_loc_3);
            }
            
        }

        private function onEntityLeft(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SEntityId;
            cache.mapui.entityLeft(_loc_2);
            if (_view && !_view.isHide)
            {
                (_view as IMapUIModule).entityLeft(_loc_2);
            }
            
        }

        private function onWindowShowHandler(event:WindowEvent) : void
        {
            (_view as IMapUIModule).updateMap(cache.mapui.currentMapId);
            (_view as IMapUIModule).updatePath(cache.mapui.mapFindPath);
            (_view as IMapUIModule).updatePostion(Game.scene.rolePlayer.currentPoint);
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new MapUIModule();
            _loc_1.addEventListener(WindowEvent.SHOW, this.onWindowShowHandler);
            return _loc_1;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.MapUpdateRolePosition, this.onMapUpdateRolePosition);
            RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onRoleMoveEnd);
            Dispatcher.addEventListener(EventName.MapFindPath, this.onMapFindPath);
            Dispatcher.addEventListener(EventName.Convey_Res, this.onConveyRes);
            Dispatcher.addEventListener(EventName.ChangeScene, this.onSceneChange);
            Dispatcher.addEventListener(EventName.ChangePosition, this.onChangePosition);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapEntity, this.onEntityEnter);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapEntityPoint, this.onEntityUpdate);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapEntityLeft, this.onEntityLeft);
            
        }

    }
}
